import 'package:flutter/material.dart';
import 'package:metaera_flutter/models/ArticleModel.dart';
import 'package:metaera_flutter/utils/Device.dart';
import 'package:metaera_flutter/utils/Font.dart';
import 'package:metaera_flutter/utils/Color.dart';
import 'package:metaera_flutter/utils/Tool.dart';
import '../routes/Router.dart';

class ArticleItem extends StatelessWidget {
  final ArticleModel articleModel;
  final VoidCallback? onTap;
  const ArticleItem({super.key, required this.articleModel, this.onTap});

  // 同时修改 showDetailTap 方法签名并接收 context
  void showDetailTap(BuildContext context, ArticleModel model) {
    Navigator.pushNamed(
      context,
      AppRoutes.articleDetail,
      arguments: {'id': model.id},
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: DeviceUtils.width18),
      child: InkWell(
        onTap: () => showDetailTap(context, articleModel),
        child: Padding(
          padding: EdgeInsets.only(
            top: DeviceUtils.width4,
            bottom: DeviceUtils.width8,
          ), // 可根据需要调整数值
          child: Column(
            children: [
              Row(
                children: [
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Padding(
                          padding: const EdgeInsets.only(bottom: 8),
                          child: // 标题
                          Text(
                            articleModel.title ?? '暂无标题',
                            style: TextStyle(
                              fontSize: AppFonts.fontSize14,
                              fontWeight: FontWeight.w600,
                              color: AppColors.color333,
                            ),
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                        Row(
                          mainAxisAlignment:
                              MainAxisAlignment.spaceBetween, // 关键：两端对齐
                          children: [
                            Row(
                              children: [
                                if (articleModel.isTop == 0)
                                  Padding(
                                    padding: EdgeInsets.only(
                                      right: DeviceUtils.width4,
                                    ),
                                    child: // 标题
                                    Text(
                                      '置顶',
                                      style: TextStyle(
                                        fontSize: AppFonts.fontSize12,
                                        color: AppColors.color6184DD,
                                      ),
                                    ),
                                  ),

                                Text(
                                  AppTool.formatTime(
                                    DateTime.parse(
                                      articleModel.releaseTime ??
                                          '2023-07-01T00:00:00.000Z',
                                    ),
                                  ),
                                  style: TextStyle(
                                    fontSize: AppFonts.fontSize12,
                                    color: AppColors.color9C9C9C,
                                  ),
                                ),
                              ],
                            ),
                            Padding(
                              padding: EdgeInsets.only(
                                right: DeviceUtils.width4,
                              ),
                              child: Container(
                                width: DeviceUtils.width100, // 指定固定宽度
                                child: Text(
                                  articleModel.author!.nickname ?? '暂无作者',
                                  style: TextStyle(
                                    fontSize: AppFonts.fontSize12,
                                    color: AppColors.color9C9C9C,
                                  ),
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  textAlign: TextAlign.right, // 文本右对齐
                                ),
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                  if (articleModel.pic != null && articleModel.pic!.isNotEmpty)
                    Padding(
                      padding: EdgeInsets.only(left: DeviceUtils.width10),
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(5), // 圆角半径
                        child: Image.network(
                          articleModel.pic ?? "",
                          width: DeviceUtils.adaptiveSizeByScreenUtil(117),
                          height: DeviceUtils.adaptiveSizeByScreenUtil(66),
                          fit: BoxFit.fill,
                        ),
                      ),
                    ),
                ],
              ),
              // 👇 添加横线
              Padding(
                padding: EdgeInsets.only(top: DeviceUtils.width18),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(5), // 圆角半径
                  child: Divider(
                    color: AppColors.colorF7F7F7, // 横线颜色
                    height: 1, // 横线高度
                    thickness: 1, // 横线粗细
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
